<?php
/**
 * NOTES 标签列表控件
 * Create by yuebin
 */
namespace haibao\cms\business\control\pc\common;

class NotesTagController extends \haibao\cms\business\control\Base{
	
	const DATA_TYPE_INDEX = 1;
	const DATA_TYPE_CHANNEL = 2;

	public static $topTagTree = array(
		310=>array('Id'=>310,'TagName'=>'时装'),
		1383=>array('Id'=>1383,'TagName'=>'明星'),
		24=>array('Id'=>24,'TagName'=>'美容'),
		955=>array('Id'=>955,'TagName'=>'品牌'),
		6863=>array('Id'=>6863,'TagName'=>'珠宝腕表'),
		7455=>array('Id'=>7455,'TagName'=>'配饰'),
		520=>array('Id'=>520,'TagName'=>'生活'),
		211=>array('Id'=>211,'TagName'=>'新娘'),
		11280=>array('Id'=>11280,'TagName'=>'美剧'),
	);
	public function __construct($updateTime = null,$controlId,$isPublish){
		parent::__construct($updateTime,$controlId,$isPublish);
	}
	
	
	/**
	 * 获取模版内容
	 * @param : String $content
	 */
	public function displayTemplateHtml(){

		$controlData = $this->getParameter(self::$CONTROL_DATA);
		$content = $this->getParameter(self::$CONTROL_CONTENT);
		$dataType = (isset($controlData['manual']['dataType']))?$controlData['manual']['dataType']:self::DATA_TYPE_CHANNEL;

		$polymerBusiness = \haibao\cms\business\control\pc\channel\polymer\PolymerController::getPolymerInstance();
		if($dataType == self::DATA_TYPE_INDEX){
			$notesTagList = array(
					array('Id'=>310,'TagName'=>'服饰风尚','Child'=>array(array('Id'=>311,'TagName'=>'服饰潮流'),array('Id'=>322,'TagName'=>'时装'),array('Id'=>334,'TagName'=>'模特'),array('Id'=>340,'TagName'=>'街拍'),array('Id'=>378,'TagName'=>'服饰搭配'),array('Id'=>397,'TagName'=>'品位男人'),array('Id'=>415,'TagName'=>'衣衣'),array('Id'=>2371,'TagName'=>'时尚杂志'),array('Id'=>4076,'TagName'=>'时装设计'),array('Id'=>6545,'TagName'=>'时尚博主'),array('Id'=>12204,'TagName'=>'报君潮流眼'),array('Id'=>9359,'TagName'=>'时装档案'),array('Id'=>11094,'TagName'=>'圈内')),
					),
					array('Id'=>1383,'TagName'=>'娇点明星','Child'=>array(array('Id'=>180,'TagName'=>'明星榜样'),array('Id'=>370,'TagName'=>'着装'),array('Id'=>1375,'TagName'=>'明星·Fashion'),array('Id'=>2265,'TagName'=>'影视剧'),array('Id'=>2272,'TagName'=>'明星街拍'),array('Id'=>2850,'TagName'=>'名流'),array('Id'=>2942,'TagName'=>'明星子女'),array('Id'=>3356,'TagName'=>'日本明星'),array('Id'=>3357,'TagName'=>'韩国明星'),array('Id'=>4660,'TagName'=>'星8卦'),array('Id'=>10996,'TagName'=>'看剧学穿衣'),array('Id'=>4443,'TagName'=>'颁奖典礼')),
					),
					array('Id'=>24,'TagName'=>'美丽达人','Child'=>array(array('Id'=>14,'TagName'=>'美发'),array('Id'=>25,'TagName'=>'美容护肤'),array('Id'=>162,'TagName'=>'彩绘纹绣'),array('Id'=>170,'TagName'=>'职场OL'),array('Id'=>252,'TagName'=>'美体'),array('Id'=>747,'TagName'=>'减肥瘦身'),array('Id'=>811,'TagName'=>'整形整容'),array('Id'=>1376,'TagName'=>'明星·Beauty'),array('Id'=>7788,'TagName'=>'美妆潮流'),array('Id'=>11012,'TagName'=>'街拍·Beauty'),array('Id'=>4287,'TagName'=>'问题肌肤'),array('Id'=>11013,'TagName'=>'美妆大片')),
					),
					array('Id'=>955,'TagName'=>'时尚品牌','Child'=>array(array('Id'=>324,'TagName'=>'品牌秀场'),array('Id'=>2826,'TagName'=>'广告大片'),array('Id'=>8192,'TagName'=>'品牌动态'),array('Id'=>5998,'TagName'=>'Lookbook'),array('Id'=>5999,'TagName'=>'新品速递'),array('Id'=>8237,'TagName'=>'品牌科普')),
					),
					array('Id'=>520,'TagName'=>'DIY·生活','Child'=>array(array('Id'=>23,'TagName'=>'旅游'),array('Id'=>519,'TagName'=>'家居饰品'),array('Id'=>521,'TagName'=>'缤纷美食'),array('Id'=>12736,'TagName'=>'星座·情感'),array('Id'=>668,'TagName'=>'运动健康'),array('Id'=>1513,'TagName'=>'Sex&Love'),array('Id'=>3067,'TagName'=>'艺术·设计'),array('Id'=>4677,'TagName'=>'宠物'),array('Id'=>5400,'TagName'=>'城市生活'),array('Id'=>6918,'TagName'=>'创意·设计'),array('Id'=>11017,'TagName'=>'美物美图')),
					),
					array('Id'=>6863,'TagName'=>'珠宝腕表','Child'=>array(array('Id'=>6872,'TagName'=>'明星与珠宝'),array('Id'=>6873,'TagName'=>'王室与珠宝'),array('Id'=>6875,'TagName'=>'珠宝流行趋势'),array('Id'=>6877,'TagName'=>'明星与腕表'),array('Id'=>8221,'TagName'=>'名品解读'),array('Id'=>8236,'TagName'=>'钟表展'),array('Id'=>8239,'TagName'=>'腕表搭配'),array('Id'=>8302,'TagName'=>'珠宝腕表大片'),array('Id'=>8992,'TagName'=>'街拍珠宝'),array('Id'=>11001,'TagName'=>'珠宝腕表明星示范')),
					),
					array('Id'=>7455,'TagName'=>'包·鞋·配饰','Child'=>array(array('Id'=>455,'TagName'=>'鞋靴'),array('Id'=>7768,'TagName'=>'明星·配饰'),array('Id'=>7838,'TagName'=>'配饰新品速递'),array('Id'=>7959,'TagName'=>'街拍配饰'),array('Id'=>8191,'TagName'=>'配饰流行趋势'),array('Id'=>8323,'TagName'=>'配饰工艺探秘'),array('Id'=>503,'TagName'=>'包包'),array('Id'=>11015,'TagName'=>'配饰大片'),array('Id'=>11015,'TagName'=>'恋物趣'),array('Id'=>11014,'TagName'=>'配饰秀场细节')),
					),
					array('Id'=>211,'TagName'=>'婚嫁·新娘','Child'=>array(array('Id'=>212,'TagName'=>'婚纱'),array('Id'=>219,'TagName'=>'结婚礼服'),array('Id'=>228,'TagName'=>'婚礼'),array('Id'=>248,'TagName'=>'新娘'),array('Id'=>251,'TagName'=>'新郎'),array('Id'=>3631,'TagName'=>'婚礼蛋糕'),array('Id'=>5263,'TagName'=>'明星婚礼'),array('Id'=>6626,'TagName'=>'婚纱灵感红毯秀'),array('Id'=>6633,'TagName'=>'蜜月'),array('Id'=>10871,'TagName'=>'情侣珠宝')),
					),
			);
			$topNoteTags = array(310=>'服饰风尚',1383=>'娇点明星',24=>'美丽达人',955=>'时尚品牌',520=>'DIY·生活',6863=>'珠宝腕表',7455=>'包·鞋·配饰',211=>'婚嫁·新娘');
			$finalList = $polymerBusiness->getNotesTagListByTopTagId(array_keys($topNoteTags));
			$finalList = $this->packageIndexNoteList($finalList,$notesTagList,$topNoteTags);
		}else{
			$topTagId = (isset($controlData['manual']['topTagId']))?$controlData['manual']['topTagId']:1383;
			$finalList = $polymerBusiness->getNotesTagListByTopTagId($topTagId,6);
			$finalList = $this->packageChannelNoteObjectList($finalList);
			$finalList = $this->getChannelNoteList($topTagId,$finalList);
		}
		$this->assign('noteList',$finalList);
		$this->assign('noteType',$dataType);
		$this->assign('notesUrl',\haibao\classlibrary\Config::getConfig(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_HAIBAO_URL).'/notes/');
		$path = $this->generateHtmlPath(__CLASS__);
		$this->generateHtml($path,$content);
		return $this->getHtml($path);
	}
	
	private function getNotesTagList($topTagId){
		$polymerBusiness = \haibao\cms\business\control\pc\channel\polymer\PolymerController::getPolymerInstance();
		$notesTagList = $polymerBusiness->getNotesTagListByTopTagId($topTagId);
		if($notesTagList){
			$notesTagList = \haibao\cms\business\Tag::getTagsById(array_map('array_shift', $notesTagList));
			print_R(array_map('array_shift', $notesTagList));
		}
		return $notesTagList;

	}
	private function packageIndexNoteList($noteList,$notesTagList,$topNoteTags){
		$noteList = array_pop($noteList);
		$resList = array();
		$polymerBusiness = \haibao\cms\business\control\pc\channel\polymer\PolymerController::getPolymerInstance();
		$childList = array_map('end',$notesTagList);
		$slideLeft = array(0,2,5,7);
		$tag = 0;
		foreach($noteList as $key=>$value){
			$resList[$tag]['TagId'] = $value->ParentTagId;
			$resList[$tag]['TagName'] = $topNoteTags[$value->ParentTagId];
			$resList[$tag]['ProImage'] = '';
			$resList[$tag]['Title'] = $value->Title;
			if($value->ProImage){
				$imageUrl = isset($value->ProImage[4]) ? $value->ProImage[4] : $value->ProImage[0];
				$resList[$tag]['ProImage'] = $polymerBusiness->getImageLink($imageUrl,400,400);
			}
			if($value->ObjectType == \haibao\cms\model\data\PolymerInfo::OBJECT_TYPE_ARTICLE){
				$resList[$tag]['Url'] = \haibao\classlibrary\Config::getConfig(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_HAIBAO_URL).'/notes/article/'.$value->PublishId.'.htm';
			}elseif($value->ObjectType == \haibao\cms\model\data\PolymerInfo::OBJECT_TYPE_IMAGE){
				$resList[$tag]['Url'] = \haibao\classlibrary\Config::getConfig(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_HOST_PIC).'/www/pics/'.$value->ObjectId.'.htm';
			}else{
				$resList[$tag]['Url'] = '';
			}
			$resList[$tag]['Child'] = $childList[$key];
			$resList[$tag]['SlideLeft'] = in_array($tag,$slideLeft) ? 1 : 0;
			$tag++;
		}
		return $resList;
	}

	private function packageChannelNoteObjectList($noteList){
		$noteList = array_pop($noteList);
		$polymerBusiness = \haibao\cms\business\control\pc\channel\polymer\PolymerController::getPolymerInstance();
		$resList = array();
		foreach($noteList as $key=>$value){
			$resList[$key]['Title'] = $value->Title;
			$resList[$key]['ProImage'] = '';
			if($value->ProImage){
				$imageUrl = isset($value->ProImage[4]) ? $value->ProImage[4] : $value->ProImage[0];
				$resList[$key]['ProImage'] = $polymerBusiness->getImageLink($imageUrl,400,400);
			}
			if($value->ObjectType == \haibao\cms\model\data\PolymerInfo::OBJECT_TYPE_ARTICLE){
				$resList[$key]['Url'] = \haibao\classlibrary\Config::getConfig(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_HAIBAO_URL).'/notes/article/'.$value->PublishId.'.htm';
			}elseif($value->ObjectType == \haibao\cms\model\data\PolymerInfo::OBJECT_TYPE_IMAGE){
				$resList[$key]['Url'] = \haibao\classlibrary\Config::getConfig(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_HOST_PIC).'/www/pics/'.$value->ObjectId.'.htm';
			}else{
				$resList[$key]['Url'] = '';
			}
		}
		return $resList;
	}

	private function getChannelNoteList($topTagId,$noteList){
		$resArr = array();
		$positions = array(4,8,10,13,18,25);
		$polymerBusiness = \haibao\cms\business\control\pc\channel\polymer\PolymerController::getPolymerInstance();
		$objectArr = array(
			\haibao\cms\model\data\PolymerInfo::OBJECT_TYPE_ARTICLE,
			\haibao\cms\model\data\PolymerInfo::OBJECT_TYPE_IMAGE
		);
		$firstTag = array('Id'=>$topTagId,'TagName'=>self::$topTagTree[$topTagId]['TagName']);
		$getPolymerList = $polymerBusiness->getPolymerListByParentTagId($topTagId,$objectArr,20);
		$tagsArr = array_merge(array($firstTag),$getPolymerList);
		$countLoop = count($tagsArr) + count($noteList);
		for($i=0;$i < $countLoop;$i++){
			if(in_array($i,$positions)){
				$resArr[$i] = array_shift($noteList);
				$resArr[$i]['DataType'] = 1;
			}else{
				$resArr[$i] = array_shift($tagsArr);
				$resArr[$i]['DataType'] = 2;
			}
		}

		$bgArr = array();
		for($i=1;$i<=9;$i++){
			$bgArr[] = $i%2 ? 1 : 0;
		}
		for($i=1;$i<=8;$i++){
			$bgArr[] = $i%2 ? 1 : 0;
		}
		for($i=1;$i<=10;$i++){
			$bgArr[] = $i%2 ? 0 : 1;
		}
		foreach($resArr as $key=>$value){
			$resArr[$key]['Color'] = $bgArr[$key];
		}

		return $resArr;
	}
}